// pages/seckill/seckill.js

const {
  toUrl,
  globalData,
  msgLoading
} = getApp();
import {
  request
} from '@/utils/request'
Page({
  toUrl,
  msgLoading,
  data: {
    pages: 1,
    count: 1,
    isExit: false,
    globalData,
    currentIndex: 0,
    menu: 0,
    form: {

    },
    timer: null,
    list: [],

  },
  onLoad(options) {
    msgLoading()
  },
  miaosha(data) {
    let {
      url,
      num,
      buy_num,
      quota_num,
      status
    } = data.currentTarget.dataset
    wx.navigateTo({
      url: url,
    })
  },
  index() {
    if (this.data.pages > 1 && this.data.pages > this.data.count) {
      this.setData({
        isExit: true,
      });
      return;
    }
    //获取列表
    const that = this
    let data = {
      is_integral: this.data.menu,
      suppliers_id: '',
      page: this.data.pages
    }
    request('seckill/goodslist', {
      data
    }).then(res => {
      let pages = that.data.pages + 1;
      let listData = this.data.list.concat(res.data)
      this.setData({
        list: listData,
        pages,
        count: res.count
      })

      if (res.data.length > 0) {
        this.timer = setInterval(() => {
          this.handleSeckill() //秒杀处理时间
        }, 1000);
      }
    }).catch((err) => {
      console.log(err);
    }).finally(() => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
  },
  handleSeckill() {
    let dataList = this.data.list
    let times = null
    dataList.forEach((it) => {
      if (it.status == 0) {
        times = it.start_time
      } else {
        times = it.end_time
      }
      if (it.status == 0) {
        if (it.dataList == undefined) {
          1
        } else {
          if (it.dataList[0] == '0' && it.dataList[1] == '0' && it.dataList[2] == '00' && it.dataList[3] == '00') {
            it.dataList = []
            times = it.end_time
            it.status = 1
          } else {
            times = it.start_time
          }
        }
      }
      if (it.status == 1) {
        if (it.dataList == undefined) {
          1
        } else {
          if (it.dataList[0] == '0' && it.dataList[1] == '0' && it.dataList[2] == '00' && it.dataList[3] == '00') {
            it.status = 2
            return
          } else {
            times = it.end_time
          }
        }
      }
      if (times == null) {
        const stime = new Date().getTime();
        let flag = stime < it.end_time
        if (flag == true) {
          it.status = 2
        } else {
          times = it.end_time
        }
      }
      let handleTimes = this.timestampToTime(times)
      it.dataList = handleTimes
    })
    this.setData({
      list: dataList,
    })
  },

  // 时间戳：1637244864707
  /* 时间戳转换为时间 */
  timestampToTime(endTime) {
    const stime = new Date().getTime();
    const etime = new Date(endTime) * 1000;
    const diffTime = etime - stime;
    const day = Math.floor(diffTime / (1000 * 60 * 60 * 24));
    const hour = Math.floor(diffTime / (1000 * 60 * 60) % 24);
    const minute = Math.floor(diffTime / (1000 * 60) % 60);
    const second = Math.floor(diffTime / 1000 % 60);

    // console.log(hour, minute, second);
    if (diffTime > 0) {
      return [day, hour, minute, second]
    } else {
      return [0, 0, '00', '00']
    }
  },
  swichTab(e) {
    clearInterval(this.timer)
    this.setData({
      timer: null
    })
    const index = e.currentTarget.dataset.index;
    if (index == this.data.currentIndex) {
      return;
    }
    const form = this.data.form;
    form.page = 1;
    form.type = index;
    this.setData({
      menu: index,
      form: form,
      pages: 1,
      count: 1,
      list: [],
      isExit: false,
      currentIndex: index,
    });
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    this.index();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: '加载中...',
      icon: 'none',
      mask: true,
    })
    this.setData({
      pages: 1,
      list: [],
      count: 1,
      isExit: false
    })
    this.index()
    console.log(1111);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('我卸载了11');
    clearInterval(this.timer)
    this.setData({
      timer: null
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.timer)
    this.setData({
      timer: null
    })
    console.log('我卸载了');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    msgLoading()
    this.setData({
      pages: 1,
      list: [],
      count: 1,
      isExit: false
    })
    this.index()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(444);
    let isExit = this.data.isExit;
    if (isExit) {
      return;
    } else {
      this.index();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})